__author__ = 'shawhen'

import platform


"""
python解释器运行在什么系统上面，比如windows 32位，linux 32位神马的。。。
(系统类型type[windows,linux],
 系统别名alias[比如windows的xp,7,2008serverr2...
              linux的centos, mac的....],
 系统版本version
    [windows的nt版本(6.1)，linux内核版本(2.6.32), ]
 系统位数arch
    [i386,amd64])
 针对各个系统的附加项目：
     windows： 构建版本号build_num(7601); ServicePack sp(sp1);
     linux:

 @NOTE
 目前测试的平台有：
 Windows XP x86 / Windows 7 x86 / Windows Server 2008 R2 Enterprise x64 / Windows 8.1 x64
 Linux-2.6.32-504.8.1.el6.x86_64-x86_64-with-centos-6.6-Final / Linux-2.6.32-504.12.2.el6.i686-i686-with-centos-6.6-Final
 测试的Python版本有python 3.4.2 x86
"""
running_os = {}
platform_text = platform.platform().lower()
machine_text = platform.machine().lower()

if "win" in platform_text:
    running_os["type"] = "windows"

    if "86" in machine_text:
        running_os["arch"] = "i386"
    elif "amd64" in machine_text:
        running_os["arch"] = "amd64"
    else:
        raise NotImplementedError

    platform_text_tuple = platform_text.split("-")
    if len(platform_text_tuple) == 3:
        platform_type, platform_alias, platform_nt_build = platform_text_tuple
    elif len(platform_text_tuple) == 4:
        platform_type, platform_alias, platform_nt_build, sp = platform_text_tuple
        running_os["sp"] = sp.lower()
    running_os["alias"] = platform_alias.lower()
    running_os["version"] = platform_nt_build[:platform_nt_build.rfind(".")]
    running_os["build_num"] = platform_nt_build[platform_nt_build.rfind(".")+1:]
elif "linux" in platform_text:
    running_os["type"] = "linux"
    platform_text_tuple = platform_text.split("-")
    running_os["version"] = platform_text_tuple[1]

    if "x86_64" in platform_text:
        running_os["arch"] = "amd64"
    elif "i686" in platform_text or "i386" in platform_text:
        running_os["arch"] = "i386"
    else:
        raise NotImplementedError

    if "centos" in platform_text:
        running_os["alias"] = "centos"
else:
    raise NotImplementedError